Skip to content

Use rust-native error for RemoveIdentityEquiv - #16195

Open
raynelfss wants to merge 1 commit into
Qiskit:mainfrom
raynelfss:use-remove-identity-error
Open

Use rust-native error for RemoveIdentityEquiv #16195
raynelfss wants to merge 1 commit into
Qiskit:mainfrom
raynelfss:use-remove-identity-error

Conversation

@raynelfss

@raynelfss raynelfss commented May 16, 2026

Copy link
Copy Markdown
Contributor

The following commits modify RemoveIdentityEquiv to use rust-native errors throughout its pipeline.
In this PR we introduce the error enumeration RemoveIdentityEquivEerror which only has two variants, one for DAGCircuitError and the other is for a specific case involving PauliEvolution gates that only exist in Python currently.

Use this to visualize the actual diff

Pre-requisites

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

@raynelfss
raynelfss requested a review from a team as a code owner May 16, 2026 14:22
@raynelfss
raynelfss requested a review from gadial May 16, 2026 14:22
@raynelfss raynelfss added on hold Can not fix yet Changelog: None Do not include in the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository mod: transpiler Issues and PRs related to Transpiler labels May 16, 2026
@qiskit-bot

Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@coveralls

coveralls commented May 16, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 26459066270

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage decreased (-0.003%) to 87.513%

Details

  • Coverage decreased (-0.003%) from the base build.
  • Patch coverage: 7 uncovered changes across 1 file (5 of 12 lines covered, 41.67%).
  • 17 coverage regressions across 2 files.

Uncovered Changes

File Changed Covered %
crates/transpiler/src/passes/remove_identity_equiv.rs 12 5 41.67%

Coverage Regressions

17 previously-covered lines in 2 files lost coverage.

File Lines Losing Coverage Coverage
crates/qasm2/src/parse.rs 12 97.15%
crates/qasm2/src/lex.rs 5 92.03%

Coverage Stats

Coverage Status
Relevant Lines: 123987
Covered Lines: 108505
Line Coverage: 87.51%
Coverage Strength: 959630.51 hits per line

💛 - Coveralls

gadial
gadial previously approved these changes May 17, 2026

@gadial gadial left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

My only question is whether the .map_err(RemoveIdentityEquivError::PyPauliRotationTraceAndDim)?; can be avoided by more implicit conversion, and whether it should be avoided like this. But it's a very minor concern.

The following commits modify `RemoveIdentityEquiv` to use rust-native errors throughout its pipeline.
In this PR we introduce the error enumeration `RemoveIdentityEquivEerror` which only has two variants, one for `DAGCircuitError` and the other is for a specific case involving `PauliEvolution` gates that only exist in Python currently.
These changes mostly depend on Qiskit#16134.
@raynelfss
raynelfss force-pushed the use-remove-identity-error branch from e3b94ca to 215c313 Compare May 26, 2026 15:48
@raynelfss raynelfss removed the on hold Can not fix yet label May 26, 2026
#[derive(Debug, thiserror::Error)]
pub enum RemoveIdentityEquivError {
#[error(transparent)]
DAGCircuit(#[from] DAGError),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the run_remove_identity_equiv function return a DAGError? Isn't it all PyPauliRotationTraceAndDim?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I think I was under the asusmption it did but it actually doesn't at all. In that case, since the only error is a Python error, should I replace it with a unique error type (a la #16133)? It sounds like a better option (we'd have to use one fixed message for the error though), but at this point I'm not sure it is much better than having it return PyResult in the end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at this further and the thing is I'm not sure what type of exception might be returned by pauli_rotation_trace_and_dimension. It's not very clear from the code itself but I can assume once PauliEvolutionGate is added to Rust (if planned) this method may become infallible. But other than that, the only solution would be to create an error that just wraps a PyErr. However, I feel that might be a bit redundant at the end of the day.

I could also make a default rust error with a generic message that maps down to a python error but I feel it might be misleading to a user trying to debug why their function call failed. Not to mention it would lose all context of its origin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. mod: transpiler Issues and PRs related to Transpiler Rust This PR or issue is related to Rust code in the repository

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

6 participants